<template>
  <div>
    <el-table :data="tableData">
      <el-table-column
        prop="id"
        label="id"
        width="140"
      >
      </el-table-column>
      <el-table-column
        prop="username"
        label="账号"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="password"
        label="密码"
      >
      </el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="Update(scope.row.id)"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="open(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data: () => ({
    tableData: []
  }),
  async created() {
    const res = await this.$http.getusers()
    this.tableData = res.message
  },
  methods: {
    Update(id) {
      this.$router.push(`/admin/edituser/${id}`)
    },
    open(id) {
      this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async () => {
        await this.$http.deleteuser(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).then(async () => {
        const res = await this.$http.getusers()
        this.tableData = res.message
        console.log(this.tableData)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
